﻿using Microsoft.Xna.Framework;
using ShevaDeferredRenderer;
using ShevaEngine.Core;
using ShevaEngine.Core.Interfaces;
using ShevaEngine.Core.Modules.Graphics;
using ShevaEngine.Core.Modules.Graphics.Cameras;
using ShevaEngine.Core.Modules.Scenes;
using ShevaEngine.Core.Modules.ParticleSystems;

namespace Demo
{
    /// <summary>Becher islands.</summary>
    public class BecherIslands : IShevaEngineGamePart
    {
        /// <summary>Viewport.</summary>
        public GraphicsViewport Viewport;
        /// <summary>Renderer.</summary>
        public IRenderer Renderer;
        /// <summary>Fly camera controller.</summary>
        public FlyCameraController FlyCameraController;

        /// <summary>
        /// Method activate.
        /// </summary>
        public void Activate()
        {
            this.Viewport = new GraphicsViewport(new Rectangle(0, 0,
                    ShevaEngine.Core.ShevaEngine.Instance.GraphicsDevice.Viewport.Width,
                    ShevaEngine.Core.ShevaEngine.Instance.GraphicsDevice.Viewport.Height));
            
            this.Viewport.Scene = new Scene();
            this.Viewport.Camera = new Camera();

            this.Viewport.Camera.ViewMatrix = Matrix.CreateLookAt(new Vector3(-70, 100, -70), Vector3.UnitY * 100, Vector3.UnitY);
            this.Viewport.Camera.CreateProjectionMatrix(this.Viewport.AspectRatio);

            this.Renderer = new DeferredRenderer(this.Viewport);
            this.Viewport.Renderer = this.Renderer;

            (this.Renderer as DeferredRenderer).ClearColor = Color.LightBlue;            

            HeightmapTerrain terrain = new HeightmapTerrain();
            terrain.AddInScene(this.Viewport.Scene);

            ParticleSystem firePSystem = ParticleSystemManager.Instance.CreateParticleSystem("Fire");        
            
            firePSystem.AddInScene(this.Viewport.Scene);

            this.FlyCameraController = new FlyCameraController(this.Viewport.Camera);
        }

        /// <summary>
        /// Method deactivate.
        /// </summary>
        public void Deactivate()
        {

        }

        /// <summary>
        /// Method Update.
        /// </summary>
        /// <param name="time"></param>
        public void Update(Microsoft.Xna.Framework.GameTime time)
        {
            this.FlyCameraController.Update(time);
        }

        /// <summary>
        /// Method Draw.
        /// </summary>
        public void Draw()
        {
            this.Renderer.CreateShadowMaps();

            ShevaEngine.Core.ShevaEngine.Instance.SpriteBatch.Begin();

            ShevaEngine.Core.ShevaEngine.Instance.SpriteBatch.Draw(this.Viewport.Draw(), this.Viewport.PositionSize, Color.White);

            ShevaEngine.Core.ShevaEngine.Instance.SpriteBatch.End();
        }
    }
}
